//
//  ViewController.swift
//  day07
//
//  Created by 杨绍鑫 on 2018/9/18.
//  Copyright © 2018年 crcnet. All rights reserved.
//

import UIKit

let identif = "cell___________"
class ViewController: UIViewController {

    let tabVC = UITableViewController.init(style: .plain)
    let navBar : UINavigationBar = {
      let bar = UINavigationBar.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 64))
       // bar.barStyle = UIBarStyle.blackTranslucent
        return bar
    }()
    
    let refreshContro : UIRefreshControl = {
        let refresh = UIRefreshControl.init()
        let attris = [NSAttributedStringKey.foregroundColor:UIColor.white]
        refresh.attributedTitle = NSMutableAttributedString.init(string: "Last updated on \(Date())", attributes: attris)
        refresh.backgroundColor  = UIColor(red:0.113, green:0.113, blue:0.145, alpha:1)
        refresh.tintColor = UIColor.white
        return refresh
    }()
    
    let favoriteEmoji = ["🤗🤗🤗🤗🤗", "😅😅😅😅😅", "😆😆😆😆😆"]
    let newFavoriteEmoji = ["🏃🏃🏃🏃🏃", "💩💩💩💩💩", "👸👸👸👸👸", "🤗🤗🤗🤗🤗", "😅😅😅😅😅", "😆😆😆😆😆" ]
    var emojiData = [String]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "emoji"
        self.view.addSubview(tabVC.tableView)
        self.view.addSubview(navBar)
        self.navBar.barStyle = UIBarStyle.blackTranslucent

    
        
        let tabView = tabVC.tableView
        tabView?.estimatedRowHeight = 60
        tabView?.rowHeight = UITableViewAutomaticDimension
        tabView?.tableFooterView = UIView.init(frame: CGRect.zero)
        tabView?.separatorStyle = UITableViewCellSeparatorStyle.none
        tabView?.backgroundColor =  UIColor(red:0.092, green:0.096, blue:0.116, alpha:1)
        
        tabView?.register(UITableViewCell.self, forCellReuseIdentifier: identif)
       
        tabView?.delegate = self
        tabView?.dataSource = self
        
        tabView?.refreshControl = refreshContro
        
        refreshContro.addTarget(self, action: #selector(ViewController.didRoadEmoji), for: UIControlEvents.valueChanged)
        
        self.emojiData = favoriteEmoji
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    @objc func didRoadEmoji() {
        self.emojiData = newFavoriteEmoji
        self.tabVC.tableView.reloadData()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            self.refreshContro.endRefreshing()
        }
    }
    
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.emojiData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identif, for: indexPath)
        cell.textLabel!.text = self.emojiData[indexPath.row]
        cell.textLabel!.textAlignment = NSTextAlignment.center
        cell.textLabel!.font = UIFont.systemFont(ofSize: 50)
        cell.backgroundColor = UIColor.clear
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        return cell
        
    }
}

